package demo.common.web;

import demo.common.model.ErrorResponseData;
import demo.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by llq on 2017/11/1.
 */
public class BaseController<E, C> {

    private static Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected static final int ERROR_CODE = 500;

    @GetMapping
    public Object paginng(C c, HttpServletRequest request, HttpServletResponse response) {
        try {
            Integer userId = HttpUtils.getCurrentUserId(request);
            return handlePaging(userId, c);
        }catch (Exception e){
            return handleException(e, request, response);
        }
    }

    protected ErrorResponseData handleException(Exception ex, HttpServletRequest request, HttpServletResponse response) {

        if(ex instanceof ServiceException) {
            response.setStatus(ERROR_CODE);
            return new ErrorResponseData(((ServiceException)ex).getStatusCode(), ex.getMessage());
        }

        logger.error(ex.getMessage(), ex);
        response.setStatus(ERROR_CODE);
        return new ErrorResponseData(ex.getMessage());

    }

    protected Object handlePaging(Integer userId, C c) {
        throw new UnsupportedOperationException();
    }

    /**
     * 创建成功后，一般要返回新建对象的id
     * @param e
     * @return
     */
    @PostMapping
    @ResponseBody
    public Object create(@RequestBody E e, HttpServletRequest request, HttpServletResponse response) {
        try {
            Integer userId = HttpUtils.getCurrentUserId(request);
            return handleCreate(userId, e);
        }catch (Exception ex){
            return handleException(ex, request, response);
        }
    }

    protected Object handleCreate(Integer userId, E e) {
        throw new UnsupportedOperationException();
    }

    @GetMapping("/{id}")
    public Object get(@PathVariable int id, HttpServletRequest request, HttpServletResponse response) {
        try {
            Integer userId = HttpUtils.getCurrentUserId(request);
            return handleGet(userId, id);
        }catch (Exception ex){
            return handleException(ex, request, response);
        }
    }

    protected Object handleGet(Integer userId, int id) {
        throw new UnsupportedOperationException();
    }

    @PutMapping("/{id}")
    public Object put(@PathVariable int id, @RequestBody E e, HttpServletRequest request, HttpServletResponse response) {
        try {
            Integer userId = HttpUtils.getCurrentUserId(request);
            return handlePut(userId, id, e);
        }catch (Exception ex){
            return handleException(ex, request, response);
        }
    }

    protected Object handlePut(Integer userId, int id, E e) {
        throw new UnsupportedOperationException();
    }

    @DeleteMapping("/{id}")
    public Object delete(@PathVariable int id, HttpServletRequest request, HttpServletResponse response) {
        try {
            Integer userId = HttpUtils.getCurrentUserId(request);
            return handleDelete(userId, id);
        }catch (Exception ex){
            return handleException(ex, request, response);
        }
    }

    protected Object handleDelete(Integer userId, int id) {
        throw new UnsupportedOperationException();
    }


}
